package com.gome.controller;


import com.gome.entity.Role;
import com.gome.exception.GlobalException;
import com.gome.service.MenuService;
import com.gome.service.RoleMenuService;
import com.gome.service.RoleService;
import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jservices/role")
public class RoleController  {
    @Autowired
    private RoleService RoleService;
    @Autowired
    private RoleMenuService RoleMenuService;
    @Autowired
    private MenuService MenuService;

    /**
     * 获取全部角色
     * @param vcRolemc 角色名称
     * @param ocMenubm  角色编码
     * @return
     * @throws GlobalException
     */
    @ResponseBody
    @RequestMapping(value="/queryRoles",method =RequestMethod.POST)
    public Result queryRoles(String vcRolemc, String ocMenubm) throws GlobalException {
        Map<String,String> map= new HashMap<String,String>();
        if(null!=vcRolemc&&!"".equals(vcRolemc)){
            map.put("vcRolemc","%"+vcRolemc+"%");
        }
        map.put("ocMenubm",ocMenubm);
        List<Role> roles = RoleService.queryRoles(map);
//        List<Role> list= new ArrayList<Role>();
//        RoleMenu rm= new RoleMenu();
//        for(Role role:roles){
//            rm.setOcMenubm(ocMenubm);
    //            rm.setOcRolebm(role.getOcRolebm());
//            List<RoleMenu> roleMenuList = RoleMenuService.selectRoleMenuByRoleMenu(rm);
//            if(null!=roleMenuList&&roleMenuList.size()>0){
//                list.add(role);
//            }
//        }


        return new Result(ResultCode.SUCCESS, null, roles);
    }

    /**
     * 获取有效角色
     * @return
     * @throws GlobalException
     */
    @ResponseBody
    @RequestMapping(value="/queryValidRoles",method =RequestMethod.POST)
    public Result queryValidRoles() throws GlobalException{

        List<Role> roles = RoleService.getValidRoles();

        return new Result(ResultCode.SUCCESS, null,roles);
    }

    @Transactional
    @ResponseBody
    @RequestMapping(value="/addRole",method =RequestMethod.POST)
    public Result addRole(@RequestParam  Role role,@RequestParam String addMenubms ) throws GlobalException{
        role.setOcCzy("admin");
        return RoleService.addRole(role,addMenubms);
    }

    @Transactional
    @ResponseBody
    @RequestMapping(value="/updateRole",method =RequestMethod.POST)
    public Result updateRole(@RequestParam  Role role,@RequestParam  String menubms) throws GlobalException{
        role.setOcCzy("admin");
        return RoleService.updateRole(role,menubms);
    }

    @Transactional
    @ResponseBody
    @RequestMapping(value="/delRole",method =RequestMethod.POST)
    public Result delRole(@RequestParam  String ocRolebm) throws GlobalException{

        return RoleService.deleteRole(ocRolebm);
    }


    @ResponseBody
    @RequestMapping(value="/queryRole",method =RequestMethod.POST)
    public Result queryRole( String ocRolebm,Boolean queryUserFlag)  throws GlobalException{

        Role role = RoleService.queryRoleWithMenu(ocRolebm,queryUserFlag);


        return new Result(ResultCode.SUCCESS, null,role);
    }

    @ResponseBody
    @RequestMapping(value="/selectMenuList",method =RequestMethod.POST)
    public Result selectMenuList( int nLevel)  throws GlobalException{

        return MenuService.selectMenuList(nLevel);
    }
}
